package com.struct{
	/**
	 * 创建者：薛恒杰
	 * 工程名称：Marbles
	 * 创建时间：Jul 30, 2012 12:02:02 PM
	 * 类说明：Object.as,
	 */
	import com.role.Base;
	import com.role.MoveBase;
	import com.role.MoveBase2;
	import com.role.Soldier;
	import com.role.Soldier2;
	import com.utils.Arrow;
	import com.utils.Arrow2;
	import com.utils.Collide;
	import com.utils.Physical;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class Soldier_Layer_ extends Sprite{
		public var soldiers:Array;//士兵数组
		public var movement:Array;//运动数组
		private var timer:Timer;//只是一个Timer
		
		private var id:uint;//当前士兵下标
		private var current:Soldier2;//当前士兵对象
		private var arrow:Arrow2;//移动提示箭头
		public function Soldier_Layer_(){
			super();
			timer = new Timer(25);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			arrow = new Arrow2();
			addChild(arrow);
			arrow.visible = false;
			soldiers = [];
			movement = [];
			id = 0;
		}
		/**
		 * 加入一个士兵到战场
		 */
		public function addSoldier(s:Soldier2):void{
			if(soldiers.indexOf(s) == -1){
				soldiers.push(s);
				addChild(s);
			}
		}
		/**
		 * 下一个士兵
		 */
		public function nextSoldier():void{
			current = soldiers[id] as Soldier2;
			current.selected(true);
			arrow.x = current.x;
			arrow.y = current.y;
			current.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			if(++id >= soldiers.length){
				id = 0;
			}
		}
		/**
		 * 移除一个士兵
		 */
		public function removeSoldier(s:Soldier):void{
			var index:int = soldiers.indexOf(s);
			if(index != -1){
				soldiers.splice(index, 1);
				removeChild(s);
			}
		}
		private function mouseDownHandler(e:MouseEvent):void{
			arrow.visible = true;
			arrow.setAngle(current.mouseX, current.mouseY);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		private function mouseMoveHandler(e:MouseEvent):void{
			arrow.setAngle(current.mouseX, current.mouseY);
		}
		private function mouseUpHandler(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			current.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			current.buttonMode = false;
			arrow.visible = false;
			current.fire(arrow.angle, arrow.speed + 5);
			movement.push(current);
			timer.start();
			current.selected(false);
			//			current = null;
		}
		
		public function execution():Boolean{
			for(var i:uint = 0; i <  movement.length; ){
				var s:Soldier2 = movement[i];
				if (movement.length > 2)trace(movement.length);
				if(s.execution()){
					++i;
					checkSoldier(s);//士兵碰撞检测
					checkWalls(s);//墙壁
					//					checkBody(s);//刚体碰撞检测
					//					checkOther(s)//静动可摧毁物体碰撞检测
					//					if(isHit){//临时这样写,之后正式代码的碰撞不需要停止运行对象
					//						movement.splice(i, 1);
					//					} else {
					//						++i;
					//					}
				} else {
					movement.splice(i, 1);
				}
			}
			return movement.length > 0;
		}
		protected function checkWalls(s:MoveBase2):void {
			if (s.x + s.radius > stage.stageWidth) {
				s.x = stage.stageWidth - s.radius;
				if (0 < s.angle < Math.PI/2){
					s.angle = Math.PI - s.angle;
				}else{
					s.angle -= 3*Math.PI - s.angle;
				}
			} else if (s.x - s.radius < 0) {
				s.x = s.radius;
				if (Math.PI < s.angle < 3*Math.PI/2){
					s.angle = 3*Math.PI - s.angle;
				}else {
					s.angle = Math.PI - s.angle;
				}
			}
			if (s.y + s.radius > stage.stageHeight) {
				s.y = stage.stageHeight - s.radius;
				if (3*Math.PI/2 < s.angle < 2*Math.PI){
					s.angle = 2*Math.PI - s.angle;
				}else{
					s.angle = 2*Math.PI - s.angle;
				}
			} else if (s.y - s.radius < 0) {
				s.y = s.radius;
				if (3*Math.PI/2 < s.angle < 2*Math.PI){
					s.angle = 2*Math.PI - s.angle;
				}else{
					s.angle = Math.PI/2 + s.angle;
				}
			}
		}
		/**
		 * 士兵碰撞检测
		 */
		public function checkSoldier(s:MoveBase2):void{
			var check:MoveBase2;
			for each(check in soldiers){
				if(check == s){
					continue;
				}
				var dx:Number = check.x - s.x;
				var dy:Number = check.y - s.y;
				var dist:Number = Math.sqrt(dx*dx + dy*dy);
				if (dist < s.radius + check.radius) {//相撞了
					s.x -= s.vx;
					s.y -= s.vy;
					var lB:MoveBase2, rB:MoveBase2;
					var mark:int;//1: b1在左侧 ;   2:b2在左侧;	3:垂直
					mark = s.x < check.x ? 1: (check.x < s.x ? 2 : 3);
					switch (mark){
						case 1:
							lB = s;
							rB = check;
							checkA(lB, rB);// 不垂直情况
							break;
						case 2:
							lB = check;
							rB = s;
							checkA(lB, rB);// 不垂直情况
							break;
						case 3:
							if (s.y < check.y){
								lB = s;
								rB = check;
							}else{
								lB = check;
								rB = s;
							}
							checkB(lB, rB);//垂直
							break;
					}
					
//					check.collide = s.collide;
//					if(check.life <= 0){
//						var index:int = soldiers.indexOf(check);
//						if(index != -1){
//							soldiers.splice(index, 1);
//						}
//					}
				}
			}
		}
		private function checkA(lB:MoveBase2, rB:MoveBase2):void{
			var angleLR:Number = Math.abs(Math.atan((rB.y - lB.y) / (rB.x - lB.x))); // 球中心连线和水平方向夹角,绝对
			//分两个园得中心连线的倾角是第一象限还是第四象限
			//分解速度到左半球坐标系
			var vxL:Number = 0,
				vyL:Number = 0,
				vxR:Number = 0,
				vyR:Number = 0;
			var isOne:Boolean = false;
			if (lB.y < rB.y){//第一象限
				vxL = lB.speed * Math.cos(lB.angle - angleLR);
				vyL = lB.speed * Math.sin(lB.angle - angleLR);
				
				vxR = rB.speed * Math.cos(rB.angle - angleLR);
				vyR = rB.speed * Math.sin(rB.angle - angleLR);
				isOne = true;
			}else{//第四象限 和 x正轴(angleLR = 0)
				vxL = lB.speed * Math.cos(lB.angle + angleLR);
				vyL = lB.speed * Math.sin(lB.angle + angleLR);
				
				vxR = rB.speed * Math.cos(rB.angle + angleLR);
				vyR = rB.speed * Math.sin(rB.angle + angleLR);
			}
			var vxL1:Number = ((lB.quality-rB.quality)*vxL+2*rB.quality*vxR)/(lB.quality+rB.quality);
			var vxR1:Number = ((rB.quality-lB.quality)*vxR+2*lB.quality*vxL)/(lB.quality+rB.quality);
			if (isNaN(vxL1)){
				trace("出现错误值");
			}
			//以上求出在左半球坐标系里, 左球和右球的速度分别是
			//vxl1 vyl 和  vxr1 vyr
			//一下计算, 返回到原笛卡尔坐标系中的速度和角度
			lB.speed = Math.abs(Math.sqrt(vxL1 * vxL1 + vyL * vyL));
			rB.speed = Math.abs(Math.sqrt(vxR1 * vxR1 + vyR * vyR));
			
			var ang:Number = 0;
			if (vxL1 == 0){
				if (vyL > 0)ang = Math.PI / 2;
				if (vyL < 0)ang = Math.PI / -2;
			}else{
				ang = Math.atan2(vyL, vxL1);
				if (ang < 0)ang += 2 * Math.PI;
			}
			ang += isOne ? angleLR : -angleLR;//旋转坐标系到笛卡尔
			lB.angle = ang;
			addMoveArr(lB);
			if (vxR1 == 0){
				if (vyR > 0)ang = Math.PI / 2;
				if (vyR < 0)ang = Math.PI / -2;
			}else{
				ang = Math.atan2(vyR, vxR1);
				if (ang < 0)ang += 2 * Math.PI;
			}
			ang += isOne ? angleLR : -angleLR;//旋转坐标系到笛卡尔
			rB.angle = ang;
			addMoveArr(rB);
		}
		private function addMoveArr(s:MoveBase2):void{
			var len:int = movement.length;
			for (var i:int = 0; i < len; i ++){
				if (movement[i] == s)return;
			}
			movement.push(s);
		}//close function
		private function checkB(lB:MoveBase2, rB:MoveBase2):void{
			var vxL:Number = 0,
				vyL:Number = lB.speed * Math.sin(lB.angle),
				vxR:Number = 0,
				vyR:Number = rB.speed * Math.sin(rB.angle);
			var vyL1:Number = ((lB.quality-rB.quality)*vxL+2*rB.quality*vxR)/(lB.quality+rB.quality);
			var vyR1:Number = ((rB.quality-lB.quality)*vxR+2*lB.quality*vxL)/(lB.quality+rB.quality);
			if (vyL1 > 0){
				lB.speed = vyL1;
				lB.angle = Math.PI/2;
			}else{
				lB.speed = -vyL1;
				lB.angle = 3*Math.PI/2;
			}
			if (vyR1 > 0){
				rB.speed = vyR1;
				rB.angle = Math.PI/2;
			}else{
				rB.speed = -vyR1;
				rB.angle = 3*Math.PI/2;
			}
		}//close function
		/**
		 * 刚体碰撞检测
		 */
		public function checkBody(s:MoveBase):Boolean{
			return false;
		}
		/**
		 * 静动可摧毁物体碰撞检测
		 */
		public function checkOther(s:MoveBase):Boolean{
			return false;
		}
		/**
		 * 物体动量换算
		 */
		public function conservationOfMomentum(base1:Base, base2:Base):void{
			var c1:Collide = base1.collide;
			var c2:Collide = base2.collide;
			var c1x:Number = c1.speed * Math.cos(c1.angle /180 * Math.PI);
			var c2x:Number = c2.speed * Math.cos(c2.angle /180 * Math.PI);
			var vx:Number = Physical.ConservationOfMomentum(c1.quality + c2.quality, c1x + c2x);
			
			var speedY:Number = c1.speed * Math.sin(c1.angle / 180 * Math.PI);
			//计算双方的 动量守恒 Physical.ConservationOfMomentum
			//获取出来以后给base1.collide和 base1.collide的碰撞系数赋值
			//			base1.collide方法需要自己写下
			//			赋值完毕后加入到 movement数组里
		}
		private function timerHandler(e:TimerEvent):void{
			if(!execution()){
				timer.stop();
				nextSoldier();
			}
		}
	}
}